using MvcContrib.UI.Grid;

namespace ETestIT.Web.Presentation
{
    public class FooterTableGridRender<T> : HtmlTableGridRenderer<T> where T:class 
    {
        private int itemsCount = 0;
        private bool finishedFirstRow = false;

        protected override void RenderStartCell(GridColumn<T> column, GridRowViewData<T> rowData)
        {
            base.RenderStartCell(column, rowData);

            if (!finishedFirstRow)
                itemsCount++;
        }

        protected override void RenderRowEnd()
        {
            base.RenderRowEnd();
            finishedFirstRow = true;
        }

        protected override void RenderBodyEnd()
        {
            base.RenderBodyEnd();
            
            var footer = string.Format(@"<tfoot>
	                        <tr>
		                        <td colspan={0}></td>
		                        <td > </td>
	                        </tr>
                        </tfoot>", itemsCount);

            if(itemsCount==0)
            {
                footer = (@"<tfoot>
	                        <tr>
		                        <td > </td>
	                        </tr>
                        </tfoot>");
            }
            
            RenderText(footer);
        }
    }
}